# -*- coding: utf-8  -*-
#!/usr/local/bin/python

__author__ = 'jian.yao'

from misc.errorcode import ERROR_CODE, ERROR_MESSAGE
from commodity.models import Commodity
from django.db.models import Q

def add_commodity(name, **kwargs):
    if not name:
        return False, ERROR_MESSAGE.NAME_IS_NULL, None
    try:
        _commodity = Commodity.objects.filter(Q(name=name))
        if _commodity:
            return False, ERROR_MESSAGE.COMMODITY_ALREADY_EXIST, _commodity
    except Commodity.DoesNotExist:
        pass

    commodity = Commodity.objects.create_commodity(name=name, **kwargs)
    return True, ERROR_MESSAGE.SUCCESS, commodity

def get_commodity_list(catatory):
    if not catatory:
        return Commodity.objects.all()
    else:
        return Commodity.objects.filter(catagory = catatory)




